<?php
/**
 * Katalina
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://katalina-kush.googlecode.com/files/license.txt
 *
 * @category   Katalina
 * @package    Katalina
 * @copyright  Copyright (c) 2009 Jonathan Kushner (http://jkushner.net)
 * @license    http://code.google.com/p/katalina-kush/wiki/license New BSD License
 * @version    $Id: $
 */

/**
 * Initialize DateTimezone
 */
date_default_timezone_set('America/New_York');

/**
 * Set error reporting
 */
error_reporting(E_ALL|E_STRICT);

/**
 * Set INI Configurations
 */
ini_set('magic_quotes_sybase', 0);
ini_set('magic_quotes_runtime', 0);
ini_set('zend.ze1_compatibility_mode', 0);

/**
 * Unset all variables populated through register_globals
 */
if (ini_get('register_globals')) {
	foreach (array($_GET, $_POST, $_COOKIE, $_ENV, $_SERVER) as $var) {
        foreach (array_keys($var) as $key) {
            unset($$key);
        }
    }
}


umask(0);

/**
 * Katalina - A Central Processor 
 *
 * @category   Katalina
 * @package    Katalina
 * @copyright  Copyright (c) 2009 Jonathan Kushner (http://jkushner.net)
 * @license    http://code.google.com/p/katalina-kush/wiki/license New BSD License
 */
final class Katalina 
{
	/**
	 * Environment Type Production
	 *
	 * @var production
	 */
	const ENV_PRODUCTION 	= 'production';
	
	/**
	 * Environment Type Development
	 *
	 * @var string
	 */
	const ENV_DEVELOPMENT 	= 'development';
	
	/**
	 * Environment Type Staging
	 *
	 * @var string
	 */	
	const ENV_STAGING		= 'staging';
	
	/**
	 * Environment Type Console
	 *
	 * @var string
	 */
	const ENV_CONSOLE	 	= 'console';
	
	/**
	 * Environment Type Default
	 * 
	 * @var string
	 */
	const ENV_DEFAULT	 	= 'default';
	
	/**
	 * Environment Type
	 *
	 * @var string
	 */
	protected static $_environment = self::ENV_PRODUCTION;
	
	/**
	 * Katalina Registry
	 *
	 * @var Zend_Registry
	 */
	protected static $_registry;
	
	/**
	 * Katalina Application
	 *
	 * @var Katalina_Core_Model_App
	 */
	protected static $_app;
	
	/**
	 * Register key,value OR Override key,value
	 *
	 * @param unknown_type $key
	 * @param unknown_type $value
	 * @param unknown_type $allowModifications
	 */
	public static function register($key,$value=null,$allowModifications=false)
	{					
		if($dataEnvelope = self::registry($key,$value,false))
		{			
			// array('_write' => '', '_data' => '')
			if(is_array($dataEnvelope))
			{					
					// r+w permissions
					$writePermissions = isset($dataEnvelope['_write']) ? 
						(bool)$dataEnvelope['_write'] : $value;
	
					// data capsule
					$contentData	  = isset($dataEnvelope['_data']) ?
						$dataEnvelope['_data'] : $value;
					
					// registered, no write, has data
					if(false === $writePermissions && ($contentData))
					{
						self::throwException($key.' is registered, however locked from editting.');
					}
					
					// unset
					self::unregister($key);
			}

			return self::register($key,$value,$allowModifications);
		}
		
		// new vals
		$value = array(
			'_write' => $allowModifications,
			'_data'  => $value
		);
		self::getRegistry()->offsetSet($key,$value);
		return ;		
	}
	
	/**
	 * Retrieve registry value or the default parameter. 
	 * 
	 * Optionally auto-register if supplied and not available. 
	 *
	 * @param unknown_type $key
	 * @param unknown_type $default
	 * @param unknown_type $autoRegister
	 */
	public static function registry($key,$default=null,$autoRegister=true)
	{
		$isRegKeyAlive = (bool) self::getRegistry()->offsetExists($key);
		if(!$isRegKeyAlive && $autoRegister){
			self::register($key,$default);
		} 
		elseif($isRegKeyAlive) {
			$default = self::getRegistry()->offsetGet($key);	
		}
		return $default;
		
	}
	
	/**
	 * Unregister key,value from registry
	 *
	 * @param unknown_type $key
	 */
	public static function unregister($key)
	{
		$regData = self::registry($key,false,false);
		if(!$regData){
			self::throwException($key.' does not exist, 
				and therefore cannot be popped off the global registry stack.'
			);
		} 
		if(is_object($regData) && method_exists($regData,'__destruct')){
			$regData->__destruct();
		}
		self::getRegistry()->offsetUnset($key);
		return;
	}
	
	/**
	 * Retrieve registry object
	 *
	 * @return Zend_Registry
	 */
	public static function getRegistry()
	{
		if(!self::$_registry instanceof Zend_Registry){
			self::$_registry = Zend_Registry::getInstance();
		}
		return self::$_registry;
	}
	
	/**
	 * Throw an exception out. Optionally by module.
	 *
	 * @param unknown_type $message
	 * @param unknown_type $module
	 */
	public static function throwException($message='',$module=null)
	{
		if($module){
			$className = 'Katalina_'.ucfirst($module).'_Exception';
		} else {
			$className = 'Kushx_Exception';
		}
		throw new $className($message);
	}
	
	public static function log()
	{
		$writer = new Zend_Log_Writer_Firebug();
		$writer->setPriorityStyle(8, 'TABLE');
		$writer->setPriorityStyle(9, 'TRACE');
		
		$logger = new Zend_Log($writer);
		$logger->addPriority('TABLE', 8);
		$logger->addPriority('TRACE', 9);
	}	
}